<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
//
// ===============================================================================
// Add-On: All Invoices v1.1
// Designed for: Zen Cart 1.3.x series
// Created by: Mathew O'Marah (www.mdodesign.co.uk)
//
// Donations:  Please support Zen Cart!  paypal@zen-cart.com  - Thank you!
// ===============================================================================

require('includes/application_top.php');

require(DIR_WS_CLASSES . 'currencies.php');
$currencies = new currencies();

include(DIR_WS_CLASSES . 'order.php');

$status = $orderStatus;
$arr_orders_id = $_POST['orders_id'];
if(!isset($arr_orders_id))
    $arr_orders_id = array("0");
if(in_array(0, $arr_orders_id)) 
    $sql_orders = "";
else
    $sql_orders = " and o.orders_id in (" . implode(',', $arr_orders_id) . ") ";

?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
    <form name="processing_products" style="margin-left: 20px;" action="processing_products.php?status=<?php echo $orderStatus; ?>" method="post">
        <select multiple="multiple" name="orders_id[]" style="height: 80px">
        <?php
        $selected = '';
        if(in_array(0, $arr_orders_id))
            $selected = 'selected';
        echo '<option value="0"'.$selected.'>Select all</option>';

        $sql = "SELECT orders_id FROM " . TABLE_ORDERS . " WHERE orders_status=" . $status;
        $orders_query = $db->Execute($sql);
        while (!$orders_query->EOF) {
            $oid = $orders_query->fields["orders_id"];                
            if($selected === 'selected')
                echo '<option value="'.$oid.'">'.$oid.'</option>';
            else if(in_array($oid, $arr_orders_id))
                echo '<option value="'.$oid.'" selected="selected">'.$oid.'</option>';
            else
                echo '<option value="'.$oid.'">'.$oid.'</option>';
            $orders_query->MoveNext();
        }
        ?>
        </select>
        <input type="submit" value="Submit" />
    </form>
    <br/>
<?php


$orders_query_raw = "select o.orders_id, o.customers_id, o.customers_name, o.payment_method,o.payment_module_code, o.shipping_module_code, o.shipping_method, o.date_purchased, o.last_modified, o.currency, o.currency_value, s.orders_status_name, m.manufacturers_name,pd.products_name,p.products_model,s.orders_status_id,op.products_quantity,
op.products_name,op.orders_products_id
                          from " . TABLE_ORDERS. " o 
                          join ".TABLE_ORDERS_STATUS . " s on 
                          o.orders_status = s.orders_status_id and s.language_id='" . $_SESSION['languages_id'] . "'
                          join ".TABLE_ORDERS_PRODUCTS." op on
                          o.orders_id = op.orders_id
                          join ".TABLE_PRODUCTS." p on 
                          op.products_id = p.products_id 
                          left join ".TABLE_MANUFACTURERS." m on
                          p.manufacturers_id = m.manufacturers_id
                          join ".TABLE_PRODUCTS_DESCRIPTION." pd on
                          p.products_id = pd.products_id and pd.language_id='" . $_SESSION['languages_id'] . "'
                          where s.orders_status_id = '" . (int)$status . "'" . $sql_orders . "
                          order by m.manufacturers_name,pd.products_name asc
                          ";         
//echo       $orders_query_raw;                 
 $orders = $db->Execute($orders_query_raw);
    while (!$orders->EOF) {    
     $repetitions =  (int)$orders->fields['products_quantity'] * (int)1;     
    for ($u = 1; $u <= $repetitions; $u++) {        
    echo $orders->fields['manufacturers_name']. '&nbsp;' . $orders->fields['products_name'];
    //get the product attributes
    $attributes = $db->Execute("select products_options, products_options_values, options_values_price,
                                           price_prefix,
                                           product_attribute_is_free
                                    from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . "
                                    where orders_id = '" . (int)$orders->fields['orders_id'] . "'
                                    and orders_products_id = '" . (int)$orders->fields['orders_products_id'] . "'");
$product_attribute = array();                                                                        
        if ($attributes->RecordCount()>0) {
            while (!$attributes->EOF) {
                $product_attribute[] = $attributes->fields['products_options_values'];
                $attributes->MoveNext();
            }
        }
    if (($k = sizeof($product_attribute)) > 0) {
        for ($j = 0; $j < $k; $j++) {
          echo '&nbsp;-----&nbsp;' . nl2br(zen_output_string_protected($product_attribute[$j]));
        }
      }
        echo '</br>';
    }
      $orders->MoveNext();
  }
?>
<br/><br/><br/><br/>
<table cellspacing="5" cellpadding="5px">
<tr><th>NACEX </th><th>CTT Expresso</th><th>CTT</th><th>SEUR</th><th>CTT Expresso Espanha</th>
</tr>
<tr>
    <td valign="top" align="center">
        <table>
            <tr>
                <td valign="top" align="center"><?php
            echo LoadShippingByModuleName(array("table6"),false,$status,"", $sql_orders);
            ?>  </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo LoadShippingByModuleName(array("table6"),true,$status,"", $sql_orders); ?>
                </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo "COB: " . LoadCountCOBShippingByModuleName(array("table6"),$status,"", $sql_orders); ?>
                </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo LoadCOBValueShippingByModuleName(array("table6"),$status,"", $sql_orders) . " &euro;"; ?>
                </td>
            </tr>
        </table>
    </td>
    <td valign="top" align="center">
        <table>
            <tr>
                <td valign="top" align="center"><?php
                    echo LoadShippingByModuleName(array("table5","table2","table3"),false,$status," or (shipping_module_code = 'freeoptions' and delivery_country <> N'España')", $sql_orders);
            ?>  </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php
                echo (int)LoadShippingByModuleName(array("table5","table2","table3"),true,$status," or (shipping_module_code = 'freeoptions' and delivery_country <> N'España')", $sql_orders);
                ?>
                </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo "COB: " . LoadCountCOBShippingByModuleName(array("table5","table2","table3"),$status," or (shipping_module_code = 'freeoptions' and delivery_country <> N'España')", $sql_orders); ?>
                </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo LoadCOBValueShippingByModuleName(array("table5","table2","table3"),$status," or (shipping_module_code = 'freeoptions' and delivery_country <> N'España')", $sql_orders) . "&euro;"; ?>
                </td>
            </tr>
        </table>
    </td>

    <td valign="top" align="center">
        <table>
            <tr>
            <td valign="top" align="center"><?php
            echo LoadShippingByModuleName(array("table","table4"),false,$status,"", $sql_orders);
            ?></td>
            </tr>
            <tr>
            <td valign="top" align="center">_______</td>
            </tr>
            <tr>
            <td valign="top" align="center">
            <?php
            echo (int)LoadShippingByModuleName(array("table","table4"),true,$status,"", $sql_orders);
            ?>
            </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo "COB: " .  LoadCountCOBShippingByModuleName(array("table","table4"),$status,"", $sql_orders); ?>
                </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo LoadCOBValueShippingByModuleName(array("table","table4"),$status,"", $sql_orders) . "&euro;"; ?>
                </td>
            </tr>
        </table>
    </td>
    <td valign="top" align="center">
    <table>
        <tr>
            <td valign="top" align="center"><?php
            echo LoadShippingByModuleName(array("table7"),false,$status, $sql_orders);
            ?></td>
        </tr>
        <tr>
            <td valign="top" align="center">_______</td>
        </tr>
        <tr>
            <td valign="top" align="center">
            <?php echo (int)LoadShippingByModuleName(array("table7"),true,$status, $sql_orders) ; ?>
            </td>
        </tr>
        <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo "COB: " . LoadCountCOBShippingByModuleName(array("table7"),$status, $sql_orders); ?>
                </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo LoadCOBValueShippingByModuleName(array("table7"),$status, $sql_orders) . "&euro;"; ?>
                </td>
            </tr>
    </table>
</td>
<td valign="top" align="center">
    <table>
        <tr>
            <td valign="top" align="center"><?php
            echo LoadShippingByModuleName(array("table8"),false,$status," or (o.shipping_module_code = 'freeoptions' and o.delivery_country = N'España')", $sql_orders);
            ?></td>
        </tr>
        <tr>
            <td valign="top" align="center">_______</td>
        </tr>
        <tr>
            <td valign="top" align="center">
            <?php echo (int)LoadShippingByModuleName(array("table8"),true,$status, " or (o.shipping_module_code = 'freeoptions' and o.delivery_country = N'España')", $sql_orders) ; ?>
            </td>
        </tr>
        <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo "COB: " . LoadCountCOBShippingByModuleName(array("table8"),$status, " or (o.shipping_module_code = 'freeoptions' and o.delivery_country = N'España')", $sql_orders); ?>
                </td>
            </tr>
            <tr>
                <td valign="top" align="center">_______</td>
            </tr>
            <tr>
                <td valign="top" align="center">
                <?php echo LoadCOBValueShippingByModuleName(array("table8"),$status, " or (o.shipping_module_code = 'freeoptions' and o.delivery_country = N'España')", $sql_orders) . "&euro;"; ?>
                </td>
            </tr>
    </table>
</td>
</tr>
</table>
<?php
function LoadShippingByModuleName($arrayName,$isCount = false,$status, $sql_shipping_country, $sql_orders = "")
{
    global $db;
    $name = "'" . implode("','",$arrayName) . "'";
    
    $shipsql = "select o.orders_id from ".TABLE_ORDERS." o
            where (o.shipping_module_code in (".$name.")". $sql_shipping_country . ") and o.orders_status=". $status . " " . $sql_orders .            
            " order by o.orders_id asc";
    $shipquery = $db->Execute($shipsql);
    if($isCount)
    return $shipquery->RecordCount();
    else    
    {    
    $orderid_ship ='';
    if ($shipquery->RecordCount()>0) {
        while (!$shipquery->EOF) {
             $data = $data=='' ? $shipquery->fields['orders_id'] : $data.'<br/>'.$shipquery->fields['orders_id'];
             $shipquery->MoveNext();
            }
    }
    return $data;
  }
}

function LoadCountCOBShippingByModuleName($arrayName,$status, $sql_shipping_country, $sql_orders = "")
{
    global $db;
    $name = "'" . implode("','",$arrayName) . "'";    
    $shipsql = "select o.orders_id from ".TABLE_ORDERS." o join " . TABLE_ORDERS_TOTAL . " ot on o.orders_id=ot.orders_id and ot.class='ot_cod_fee' 
        where (o.shipping_module_code in (".$name.")" . $sql_shipping_country . ") and o.orders_status=".$status . $sql_orders;        
    
    $shipquery = $db->Execute($shipsql);
    
    return $shipquery->RecordCount();    
}

function LoadCOBValueShippingByModuleName($arrayName,$status, $sql_shipping_country, $sql_orders = "")
{
    global $db;
    $name = "'" . implode("','",$arrayName) . "'";
    
    $shipsql = "select o.order_total from ".TABLE_ORDERS." o join " . TABLE_ORDERS_TOTAL . " ot on o.orders_id=ot.orders_id and ot.class='ot_cod_fee' 
        where (o.shipping_module_code in (".$name.")". $sql_shipping_country . ") and o.orders_status=".$status.$sql_orders;
        
    $shipquery = $db->Execute($shipsql);
    $total=0;
    if ($shipquery->RecordCount()>0) {
        while (!$shipquery->EOF) {
             $total += (float)$shipquery->fields['order_total'];
             $shipquery->MoveNext();
            }
    }
    return $total;  
}
?>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
